package com.example.administrator.test.login;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.test.R;
import com.example.administrator.test.login.bean.CodeInformationSendBean;
import com.example.administrator.test.login.bean.CodeInformationTurnBean;
import com.google.gson.Gson;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.GsonConverterFactory;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 *登录界面
 */
public class LoginActivity extends AppCompatActivity {
    private EditText editText;//文本框，输入手机号码
    private Button nextStep;//下一步按钮
    private String resultCode;
    private String accState;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        editText= (EditText) findViewById(R.id.login_edittext);
        nextStep= (Button) findViewById(R.id.login_next_step);
//        if (editText.getText().length()!=0){
//            nextStep.setBackgroundColor(Color.YELLOW);
//        }
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                nextStep.setBackgroundColor(getResources().getColor(R.color.appColor));
            }
        });
        nextStep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CodeInformationSendBean sendInformationBean=new CodeInformationSendBean();
                //对需要传递给后台的信息进行初始化
                sendInformationBean=initData(sendInformationBean);
                //将bean对象转化成json格式
                Gson gson=new Gson();
                String route=gson.toJson(sendInformationBean);
                Retrofit retrofit=new Retrofit.Builder()
                        .baseUrl(Constant.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                PostRoute postRoute=retrofit.create(PostRoute.class);
                RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),route);
                Call<CodeInformationTurnBean> call=postRoute.postFlyRoute(body);
                call.enqueue(new Callback<CodeInformationTurnBean>() {
                    @Override
                    public void onResponse(Call<CodeInformationTurnBean> call, final Response<CodeInformationTurnBean> response) {
                        CodeInformationTurnBean turnInformationBean=response.body();
                        resultCode =turnInformationBean.getResultCode();
                        accState=turnInformationBean.getAccState();//后台返回的判断验证码
//                        phone=editText.getText().toString();
                        if (accState.equals("0")||accState.equals("1")){
                            if (resultCode.equals("0")){
                                //验证成功后跳转到输入验证码界面
                                Intent intent=new Intent(LoginActivity.this,CodeActivity.class);
                                intent.putExtra("phone",editText.getText().toString());
                                startActivity(intent);
                                Toast.makeText(LoginActivity.this,
                                        "phone:"+editText.getText().toString()+"resultCode"+resultCode, Toast.LENGTH_SHORT).show();
                            } else {
                                //如果判断不正确，则弹出提示内容
                                Toast.makeText(LoginActivity.this,"手机号码输入有误", Toast.LENGTH_SHORT).show();
                            }
                        }
                        //将json信息显示在textView上
//                        textView.setText(turnInformationBean.toString());
                    }
                    @Override
                    public void onFailure(Call<CodeInformationTurnBean> call, Throwable t) {
                        //如果判断不正确，则弹出提示内容
                        Toast.makeText(LoginActivity.this,"获取验证码失败", Toast.LENGTH_SHORT).show();
                    }
                });

            }
        });
    }
    @Override
    protected void onStop() {
        super.onStop();
        finish();
    }

    private CodeInformationSendBean initData(CodeInformationSendBean sendInformationBean){
        if (editText.getText().length()==11){
            sendInformationBean.setPhone(editText.getText().toString());
            sendInformationBean.setOperType("2001");
        }
        return sendInformationBean;
    }

}
